from django.contrib.auth.decorators import login_required
from django.shortcuts import render_to_response
from django.template import RequestContext
from django.http import Http404, HttpResponseRedirect
from django.contrib import messages

from apps.course.functions import course_reverse
from apps.course.models import Course
from models import *

import logging

logger = logging.getLogger('pet.apps.help.views')

def FAQ_view(request, course=None):
	logger.fxn_entry('course %s',course)
	TheCourse = Course.find_or_404(course)
	
	if not request.user.is_authenticated():
		messages.info(request,"Access to this part of the site requires authentication")
		logger.forced_redirect('Unauthenticated user trying to view help')
		return HttpResponseRedirect(course_reverse('course_index',course))

	FAQs = FAQ.find(TheCourse.course_id)
	logger.fxn_exit('Returning help contents')
	return render_to_response('help/FAQ.html',{'FAQs':FAQs}, context_instance=RequestContext(request))

def FAQ_popup(request, faqid, course=None):
	logger.fxn_entry('faqid %s, course %s', faqid, course)
	TheCourse = Course.find_or_404(course)

	if not request.user.is_authenticated():
		messages.info(request,"Access to this part of the site requires authentication");
		logger.forced_redirect('Unauthenticated user trying to view popup help')
		return HttpResponseRedirect(course_reverse('course_index',course))

	TheFAQ = FAQ.find_or_404(course=TheCourse.course_id,question_link=faqid)
	logger.fxn_exit('Returning popup help contents')
	return render_to_response('help/FAQ_popup.html',{'FAQ':TheFAQ}, context_instance=RequestContext(request))
